var express = require('express');
var axios = require('axios')
var router = express.Router();
let request = require('request')
const sha1 = require('sha1')
let send_wx_msg = require('../utils/wx_msg_tpl')
router.all('/api/wx/share1', async (req, res) => {
    let url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx838cf54dd030d93a&secret=b7f8aca56652260930307a1597e349ab'


    request(url, function (error, response, data) {

        res.send(data)
    });

});

router.all('/api/wx/share2', function (req, res) {
    let token = req.query.token;
    let url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' + req.query.token + '&type=jsapi'
    request(url, function (error, response, data) {

        res.send(data)
    });

})


router.all('/api/wx/share3', function (req, res) {
    let noncestr = 'azxrqwrdaf' + new Date().now;
    let jsapi_ticket = req.query.jsapi_ticket;
    let timestamp = new Date().getTime();
    let url = req.query.url;

    let a = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + noncestr + "&timestamp=" + timestamp + "&url=" + url;

    let oppId = 'wx838cf54dd030d93a';
    let signature = sha1(a)

    let result = {
        noncestr, jsapi_ticket, timestamp, signature, oppId, a, url
    }
    res.send(result)


})

router.all('/wx/message', function (req, res) {
    /**
     *
     * @type {User.openid|{type, comment}}
     */
    let touser = req.query.openid;
    let Turl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx838cf54dd030d93a&secret=b7f8aca56652260930307a1597e349ab'
    request(Turl, function (error, response, data) {

        let url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' + (JSON.parse(data)).access_token
        let temList = 'https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=' + (JSON.parse(data)).access_token
        let access_token = (JSON.parse(data)).access_token

        request(temList, function (error, response, body) {

            res.json(JSON.parse(body))
            // var d = send_wx_msg.task_create_success("obV1T531QSVBseNf6nXXuq1sr0AU","上海某广告位二维码扫码应用开发需求","需求状态：需求已发布，等待服务商响应")
            let d = {};
            let m = req.query.model;
            console.log(m)
            if (m == 'tcs') {//任务发布成功提醒
                let touser = req.query.openid;
                let task_title = req.query.task_title;
                let task_id = req.query.task_id;
                d = send_wx_msg.task_create_success(touser, task_title, task_id)
            } else if (m == 'signC') {//报名成功,通知报名者
                let touser = req.query.openid;
                d = send_wx_msg.sign_success(touser);
            } else if (m == 'signT') {//报名通知甲方
                let touser = req.query.openid;
                let nickname = req.query.nickname;
                d = send_wx_msg.sign_notice(touser, nickname)
            }else if(m =='signG'){//报名授权通知
                let touser = req.query.openid;
                d = send_wx_msg.sign_get(touser)
            }else if(m =='tastFO'){//乙方完成,通知甲方
                let touser = req.query.openid;
                d = send_wx_msg.task_finish_one(touser)
            }else if(m =='tastFT'){//甲方完成,通知乙方
                let touser = req.query.openid;
                d = send_wx_msg.task_finish_two(touser)
            }else if( m =='u_w_m'){
                let touser = req.query.openid;
                let nickname = req.query.nickname;
                let amount = req.query.amount;
                d = send_wx_msg.user_w_message(touser,nickname,amount)
            }else if(m =='sign_fail'){
                let touser = req.query.openid;
                let id = req.query.id;
                d = send_wx_msg.sign_fail(touser,id)
            }

            request({
                url: url,
                method: "POST",
                json: true,
                body: d
            }, function (error, response, body) {

                // res.send(body)
            })
        })


    })
})
module.exports = router;

